//
//  DFStatusCell.swift
//  FlyBlog
//
//  Created by FEI on 16/4/14.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit

import SnapKit
class DFStatusCell: UITableViewCell {

    var bottomViewTopCons: Constraint?

    var status: DFStatus? {
        didSet {
            originalView.status = status

            bottomViewTopCons?.uninstall()

            if let retweetStatus = status?.retweeted_status {
                retweetView.hidden = false
                retweetView.retweetStatus = retweetStatus

                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else {
                retweetView.hidden = true

                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        prepareUI()
    }

    private func prepareUI() {
         //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetView)

        //添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.right.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }

        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }

        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(37)
        }

        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }

    }


    //MARK:懒加载
    private lazy var originalView: DFOriginalView = DFOriginalView()

    private lazy var bottomView: DFStatusBottomView = DFStatusBottomView()

    private lazy var retweetView: DFStatusRetweetedView = DFStatusRetweetedView()
}
